package com.sctu.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.sctu.EMSApp.R;
import com.sctu.customView.CustomViewPager;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhu on 2015/11/3.
 */
public class PlayFragment  extends Fragment implements View.OnClickListener{
    private View view;
    private RelativeLayout food,amuse,hotel,route,special;
    private Fragment foodFragment,amuseFragment,hotelFragment,routeFragment,specialFragment;
    private TextView foodText,amuseText,hotelText,routeText,specialText;
    private List<Fragment> fragmentList;
    private CustomViewPager viewPager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.fragment_play,container,false);
            initView();
            initClick();
            initFragment();
            deal();
        }

        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }


    private void initView() {
        food = (RelativeLayout) view.findViewById(R.id.play_food_top);
        amuse = (RelativeLayout) view.findViewById(R.id.play_amuse_top);
        hotel = (RelativeLayout) view.findViewById(R.id.play_hotel_top);
        route = (RelativeLayout) view.findViewById(R.id.play_route_top);
        special = (RelativeLayout) view.findViewById(R.id.play_special_top);

        foodText = (TextView) view.findViewById(R.id.play_food_top_text);
        amuseText = (TextView) view.findViewById(R.id.play_amuse_top_text);
        hotelText = (TextView) view.findViewById(R.id.play_hotel_top_text);
        routeText = (TextView) view.findViewById(R.id.play_route_top_text);
        specialText = (TextView) view.findViewById(R.id.play_special_top_text);

        viewPager = (CustomViewPager) view.findViewById(R.id.play_viewpager);
    }

    private void initClick() {
        food.setOnClickListener(this);
        amuse.setOnClickListener(this);
        hotel.setOnClickListener(this);
        route.setOnClickListener(this);
        special.setOnClickListener(this);


    }

    private void initFragment() {
        foodFragment = new PlayFoodFragment();
        amuseFragment = new PlayAmuseFragment();
        hotelFragment = new PlayHotelFragment();
        routeFragment = new PlayRouteFragment();
        specialFragment = new PlaySpecialFragment();

        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(foodFragment);
        fragmentList.add(amuseFragment);
        fragmentList.add(hotelFragment);
        fragmentList.add(routeFragment);
        fragmentList.add(specialFragment);
    }

    private void deal() {

        viewPager.setAdapter(new MyFrageStatePagerAdapter(getActivity().getSupportFragmentManager()));
        setDefault();
    }

    private void setDefault(){

        changeBack(food);
        changeText(foodText);
        viewPager.setCurrentItem(0);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_food_top:
                changeBack(food);
                changeText(foodText);
                viewPager.setCurrentItem(0);
                break;
            case R.id.play_amuse_top:
                changeBack(amuse);
                changeText(amuseText);
                viewPager.setCurrentItem(1);
                break;
            case R.id.play_hotel_top:
                changeBack(hotel);
                changeText(hotelText);
                viewPager.setCurrentItem(2);
                break;
            case R.id.play_route_top:
                changeBack(route);
                changeText(routeText);
                viewPager.setCurrentItem(3);
                break;
            case R.id.play_special_top:
                changeBack(special);
                changeText(specialText);
                viewPager.setCurrentItem(4);
                break;
        }
    }

    private void changeBack(RelativeLayout view){
        food.setBackgroundResource(R.drawable.top_background);
        amuse.setBackgroundResource(R.drawable.top_background);
        hotel.setBackgroundResource(R.drawable.top_background);
        route.setBackgroundResource(R.drawable.top_background);
        special.setBackgroundResource(R.drawable.top_background);

        view.setBackgroundResource(R.drawable.top_background_on);
    }

    private void changeText(TextView view){

        foodText.setTextColor(getResources().getColor(R.color.green));
        amuseText.setTextColor(getResources().getColor(R.color.green));
        hotelText.setTextColor(getResources().getColor(R.color.green));
        routeText.setTextColor(getResources().getColor(R.color.green));
        specialText.setTextColor(getResources().getColor(R.color.green));

        view.setTextColor(getResources().getColor(R.color.white));
    }


    private class MyFrageStatePagerAdapter extends FragmentStatePagerAdapter {

        public MyFrageStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {

            return fragmentList.get(i);
        }


        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);

        }

    }

    @Override

    public void onDetach() {

        super.onDetach();

        try {

            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");

            childFragmentManager.setAccessible(true);

            childFragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {

            throw new RuntimeException(e);

        } catch (IllegalAccessException e) {

            throw new RuntimeException(e);

        }

    }

}
